package com.chexiang360.controller;

import com.chexiang360.common.constant.SystemConstant;
import com.chexiang360.common.entity.CheckResult;
import com.chexiang360.common.util.JwtUtils;
import com.chexiang360.dto.Result;
import com.chexiang360.ex.ServiceException;
import com.chexiang360.model.UserAdmin;
import com.chexiang360.service.UserAdminService;
import com.chexiang360.util.MD5Util;
import com.chexiang360.util.TUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class ManageController extends BaseController {


    @Autowired
    UserAdminService userAdminService;

    @Autowired
    JwtUtils jwtUtils;

    @ApiOperation(value = "登录")
    @ApiImplicitParams({ @ApiImplicitParam(name = "username", value = "账号(手机号)", required = true), @ApiImplicitParam(name = "password", value = "密码", required = true) })
    @PostMapping("/login")
    public Result<Map<String, String>> login(String username, String password) throws Exception {
        TUtil.checkObj(new String[] { "账号为空", "密码为空" }, username, password);

        UserAdmin login = userAdminService.login(username, MD5Util.md5(password));

        if (null == login) {
            throw new ServiceException(SystemConstant.RESCODE_LOGIN , "账号或密码错误");
        }

        Map<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("phone", login.getPhone());
        hashMap.put("name", login.getName());
        hashMap.put("roleId", ""+login.getRoleid());

        //把token返回给客户端-->客户端保存至cookie-->客户端每次请求附带cookie参数
        String JWT = jwtUtils.createJWT(""+login.getId(), hashMap.toString(), SystemConstant.JWT_TTL);
        paramMap.put("phone", login.getPhone());
        paramMap.put("name", login.getName());
        paramMap.put("roleId", ""+login.getRoleid());
        paramMap.put("token", JWT);
        return Result.initSuccessResult(paramMap);
    }

    /**
     *  验证登录状态
     * @param token
     * @return
     */
    @PostMapping("/checkResult")
    public Result<String> CheckResult(String token) {
        //验证JWT的签名，返回CheckResult对象
        CheckResult checkResult = JwtUtils.validateJWT(token);
        if (checkResult.isSuccess()) {
            return Result.initSuccessResult(checkResult.getClaims().getSubject());
        } else {
            switch (checkResult.getErrCode()) {
                // 签名验证不通过
                case SystemConstant.JWT_ERRCODE_FAIL:
                    log.info("签名验证不通过");

                    Result.initFailureResult(SystemConstant.JWT_ERRCODE_FAIL, "签名验证不通过");
                    break;
                // 签名过期，返回过期提示码
                case SystemConstant.JWT_ERRCODE_EXPIRE:
                    log.info("签名过期");
                    Result.initFailureResult(SystemConstant.JWT_ERRCODE_EXPIRE, "签名过期!!");
                    break;
                default:
                    break;
            }
            return Result.initFailureResult(SystemConstant.JWT_ERRCODE_NULL, "签名验证不通过, 请重新获取!!!");
        }
    }
}
